<template>
  <div class="table-page">
    <a-form label-align="right" auto-label-width :model="form" class="form">
      <a-row :gutter="16" wrap>
        <a-col :xs="12" :md="12" :lg="8" :xl="6" :xxl="6">
          <a-form-item field="mobile" label="用户手机号">
            <a-input v-model="form.mobile" placeholder="请输入手机号码" />
          </a-form-item>
        </a-col>
        <a-col :xs="12" :md="12" :lg="8" :xl="6" :xxl="6">
          <a-form-item field="nickname" label="用户昵称">
            <a-input v-model="form.nickname" placeholder="请输入姓名" />
          </a-form-item>
        </a-col>
        <a-col :xs="12" :md="12" :lg="8" :xl="6" :xxl="6">
          <a-form-item field="rangeValue" label="交易时间">
            <a-range-picker v-model="form.rangeValue" />
          </a-form-item>
        </a-col>
        <a-col :xs="12" :md="12" :lg="8" :xl="6" :xxl="6">
          <a-form-item field="way_in" label="类型">
            <a-select :style="{ width: '320px' }" v-model="form.way_in" placeholder="请选择类型" allow-clear>
              <a-option value="1">账户充值</a-option>
              <a-option value="2">后台充值</a-option>
              <a-option value="3">任务扣费</a-option>
              <a-option value="4">后台扣除</a-option>
              <a-option value="5">任务退回</a-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :xs="12" :md="12" :lg="8" :xl="6" :xxl="6">
          <a-form-item field="pay_type" label="交易方式">
            <a-select :style="{ width: '320px' }" v-model="form.pay_type" placeholder="请选择类型" allow-clear>
              <a-option value="1">微信</a-option>
              <a-option value="2">支付宝</a-option>
              <a-option value="3">系统</a-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-space>
            <a-button type="primary" @click="getTableData">
              <template #icon>
                <icon-search />
              </template>
              <template #default>查询</template>
            </a-button>
            <a-button @click="handleReset">
              <template #default>重置</template>
            </a-button>
          </a-space>
        </a-col>
      </a-row>
    </a-form>

    <section class="table-box">
      <a-table
        row-key="id"
        size="small"
        page-position="bl"
        :bordered="{ cell: true }"
        :loading="loading"
        :data="tableData"
        :scroll="{ x: '100%', y: '100%', minWidth: 1000 }"
        :pagination="{ showPageSize: true, showTotal: true, total: total, current: current, pageSize: pageSize }"
        @page-change="changeCurrent"
        @page-size-change="changePageSize"
      >
        <template #columns>
          <a-table-column title="用户昵称" data-index="nickname" :width="120">
            <template #cell="{ record }">
              <span>{{ record.FormCustomer.nickname }}</span>
            </template></a-table-column
          >
          <a-table-column title="头像" :width="80">
            <template #cell="{ record }">
              <a-avatar>
                <img
                  v-if="record.FormCustomer.headimage && record.FormCustomer.headimage !== ' '"
                  :src="record.FormCustomer.headimage"
                />
                <img v-else alt="avatar" src="@/assets/images/img／defaultAvatar@2x.png" />
              </a-avatar>
            </template>
          </a-table-column>
          <a-table-column title="业务单号" :min-width="150" data-index="order_num">
            <template #cell="{ record }">
              <span>{{ record.order_show.order_num }}</span>
            </template>
          </a-table-column>
          <a-table-column title="交易方式" :width="100" data-index="pay_type">
            <template #cell="{ record }">
              <div>
                <a-tag v-if="record.pay_type == 1" color="green">微信</a-tag>
                <a-tag v-if="record.pay_type == 2" color="arcoblue">支付宝</a-tag>
                <a-tag v-if="record.pay_type == 3">系统</a-tag>
              </div>
            </template>
          </a-table-column>
          <a-table-column title="类型" data-index="type" :width="100">
            <template #cell="{ record }">
              <span>{{ typeMap[record.order_show.type] }}</span>
            </template>
          </a-table-column>
          <a-table-column title="交易时间" data-index="create_time" :width="180"></a-table-column>
          <a-table-column title="变动金额" data-index="change_money" :width="120">
            <template #cell="{ record }">
              <a-typography-text v-if="record.numerial_status == '+'" type="success">{{
                record.numerial_status + record.numerial_number
              }}</a-typography-text>
              <a-typography-text v-else type="danger">{{
                record.numerial_status + record.numerial_number
              }}</a-typography-text>
            </template>
          </a-table-column>
          <a-table-column title="账户金额" :width="120" data-index="balance_after_transaction"></a-table-column>
          <a-table-column title="交易方单号" :min-width="150" data-index="pay_num">
            <template #cell="{ record }">
              <span>{{ record.order_show.pay_num }}</span>
            </template>
          </a-table-column>
          <a-table-column title="备注" :min-width="200" data-index="remarks"></a-table-column>
        </template>
      </a-table>
    </section>
  </div>
</template>

<script setup lang="ts" name="MainTable">
import { reactive, ref } from 'vue'
import { usePagination } from '@/hooks'
import { getRecordList } from '@/apis'
import { useRouter } from 'vue-router'
import { Message } from '@arco-design/web-vue'
import { format } from 'date-fns'

const router = useRouter()
const form = reactive({
  mobile: '',
  nickname: '',
  rangeValue: [],
  way_in: '',
  pay_type: ''
})
const typeMap = {
  1: '账户充值',
  2: '后台充值',
  3: '任务扣费',
  4: '后台扣除',
  5: '任务退回'
}
const loading = ref(false)
const tableData = ref([])

const { current, pageSize, total, changeCurrent, changePageSize, setTotal } = usePagination(() => getTableData())

const getTableData = async () => {
  loading.value = true
  const where = {
    page: current.value,
    limit: pageSize.value,
    ...form
  }
  if (form.rangeValue.length > 0) {
    where.pay_time_start = format(form.rangeValue[0], 'yyyy-MM-dd 00:00:00')
    where.pay_time_end = format(form.rangeValue[1], 'yyyy-MM-dd 23:59:59')
  }
  const res = await getRecordList(where)
  let resData = JSON.parse(res)

  if (resData.code === 200) {
    tableData.value = resData.result.data
    setTotal(resData.result.total)
    loading.value = false
  } else {
    loading.value = false
  }
}
const handleReset = () => {
  form.mobile = ''
  form.nickname = ''
  form.rangeValue = []
  form.way_in = ''
  form.pay_type = ''
  getTableData()
}

getTableData()
</script>

<style lang="scss" scoped>
:deep(.arco-grid) {
  flex-wrap: wrap;
}
:deep(.arco-grid-item) {
  min-width: 250px;
}
:deep(.arco-alert-success) {
  padding: 14px 16px;
  border-color: rgb(var(--success-6));
  background-color: rgba(var(--success-6), 0.08);
  .arco-alert-content {
    color: rgb(var(--success-6));
    font-size: 12px;
  }
}
.table-page {
  height: 100%;
  overflow: hidden;
  margin: $margin;
  background: var(--color-bg-1);
  padding: $padding;
  padding-bottom: 0;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  .form {
    margin-top: 12px;
    :deep(.arco-form-item) {
      margin-bottom: 10px;
    }
  }
  .table-box {
    flex: 1;
    overflow: hidden;
    margin-top: 12px;
    padding-bottom: 16px;
  }
}
</style>
